package com.zbb.comm.service.impl;

import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zbb.base.model.Result;
import com.zbb.comm.controller.WebSocketServer;
import com.zbb.comm.feignclient.UserServiceClient;
import com.zbb.comm.mapper.AssociationUserMapper;
import com.zbb.comm.po.AssociationUser;
import com.zbb.comm.po.User;
import com.zbb.comm.service.AssociationUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class AssociationUserServiceImpl implements AssociationUserService {

    @Autowired
    AssociationUserMapper associationUserMapper;

    @Autowired
    WebSocketServer webSocketServer;

    @Autowired
    UserServiceClient userServiceClient;
    @Override
    public List<AssociationUser> selectAssociationUser(String userId) {
        QueryWrapper<AssociationUser> associationUserQueryWrapper = new QueryWrapper<>();
        associationUserQueryWrapper.eq("current_user_id", userId);
        List<AssociationUser> associationUsers = associationUserMapper.selectList(associationUserQueryWrapper);
        return associationUsers;
    }

    //当出现Exception时，进行事务回滚
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result addAssUser(String assUserId, String orderDetailId, String userId) {
        //存入数据库
        associationUserMapper.insert(new AssociationUser(UUID.randomUUID().toString(),userId,assUserId,orderDetailId, LocalDateTime.now()));
        associationUserMapper.insert(new AssociationUser(UUID.randomUUID().toString(),assUserId,userId,orderDetailId, LocalDateTime.now()));
        //向自己推送新增联系人消息
        User user = userServiceClient.querytUser(assUserId);
        User myUser = userServiceClient.querytUser(userId);
//        判断联系人是否在线
        if (webSocketServer.sessionMap.containsKey(assUserId)){
            //在线则推送当前用户的信息给他
            user.setLoginState(true);
            myUser.setLoginState(true);
            JSONObject result = new JSONObject();
            JSONObject jsonObject = new JSONObject();
            jsonObject.set("userId", userId);
            jsonObject.set("userInfo", myUser);
            result.set("addUser", jsonObject);
            webSocketServer.sendMessage(JSON.toJSONString(result), webSocketServer.sessionMap.get(assUserId));
        }
        //推送他的消息给我
        JSONObject result1 = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.set("userId", assUserId);
        jsonObject1.set("userInfo", user);
        result1.set("addUser", jsonObject1);
        webSocketServer.sendMessage(JSON.toJSONString(result1), webSocketServer.sessionMap.get(userId));
        return null;
    }
}
